'''
Created on Aug 30, 2011

@author: Reprise
'''
import os,sys
import binascii

class DirWalker(object):
    
    def __init__(self, csvfile):
        self.datafile = csvfile
        self.alllines = ""

    def walk(self,origdir,dir,meth):
        dir = os.path.abspath(dir)
        for file in [file for file in os.listdir(dir) if not file in [".",".."]]:
            nfile = os.path.join(dir,file)
            if os.path.isdir(nfile):
                self.walk(origdir,nfile,meth)
            else:
                meth(nfile,origdir)    

    def concat(self,path,origdir):
        f = open(path)
        timestamp = f.readline()
        f.close()
        info = (path.replace(origdir,"")).replace("\\","")
        PartnerID = info[:len(info) - 32]
        MD5String = info[len(PartnerID):len(info)]
        bin16 = binascii.unhexlify(MD5String);
        line = PartnerID + "," + bin16 + "," + timestamp
        self.alllines += line 
        
    def writetofile(self):
        fw = open(self.datafile,'w')
        fw.write(self.alllines)
        fw.close()


dir = r"c:\Development\xfs\gate-1"
myWalker = DirWalker("c:\data.txt")
myWalker.walk(dir,dir, myWalker.concat)
myWalker.writetofile()
 